<template>
  <div v-if="visible"
       class='edit-position-dialog-container'>
    <el-dialog @close="dialogClose"
               :visible.sync="visible"
               :title="dialogStatus==='edit'?'修改定位':'新增定位'">
      <el-form :model="editPositionData"
               ref="editPositionForm"
               label-width="100px"
               :rules="editPositionRules">
        <el-row>
          <el-form-item prop="gps_num"
                        label="设备编号">
            <el-input v-model="editPositionData.gps_num"
                      placeholder="请输入设备编号"></el-input>
          </el-form-item>
          <el-form-item prop="model"
                        label="设备型号">
            <el-input v-model="editPositionData.model"
                      placeholder="请输入设备型号"></el-input>
          </el-form-item>
          <el-form-item prop="imei"
                        label="IMEI">
            <el-input v-model="editPositionData.imei"
                      placeholder="请输入IMEI"></el-input>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogClose">取 消</el-button>
        <el-button type="primary"
                   @click="editPosition">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { BdPositionApi } from '@/services/EquipmentManage'
export default {
  data () {
    return {
      visible: false,
      editPositionData: {
        gps_num: '',
        model: '',
        imei: ''
      },
      resetForm: {
        gps_num: '',
        model: '',
        imei: ''
      },
      editPositionRules: {
        gps_num: [
          { required: true, message: '请输入设备编号', trigger: 'change' }
        ]
      }
    }
  },
  props: {
    dialogVisible: Boolean,
    dialogStatus: String,
    dialogForm: Object,
    callback: Function
  },
  methods: {
    dialogClose () {
      this.$emit('update:dialogVisible', false)
    },
    // 新增或修改定位操作
    editPosition () {
      this.$refs.editPositionForm.validate((valid) => {
        if (valid) {
          if (this.dialogStatus === 'create') {
            this.createPosition()
          } else if (this.dialogStatus === 'edit') {
            this.updatePosition()
          }
        }
      })
    },
    // 新增北斗定位
    createPosition () {
      BdPositionApi.createGps({
        data: this.editPositionData
      }).then(res => {
        // console.log(res, 'create position')
        if (res.code === 0) {
          this.$message.success('新增北斗定位成功')
          this.$emit('callback', 'success callback')
          this.dialogClose()
        }
      })
    },
    // 修改北斗定位
    updatePosition () {
      BdPositionApi.updateGps({
        data: this.editPositionData
      }).then(res => {
        // console.log(res, 'update position')
        if (res.code === 0) {
          this.$message.success('修改北斗定位成功')
          this.$emit('callback', 'success callback')
          this.dialogClose()
        }
      })
    }
  },
  watch: {
    dialogVisible (newval, oldval) {
      // console.log(newval, 'watch')
      this.visible = newval
    },
    dialogForm: {
      handler (newval, oldval) {
        if (Object.keys(newval).length > 0) {
          this.editPositionData = JSON.parse(JSON.stringify(newval))
        } else {
          this.editPositionData = Object.assign(this.editPositionData, this.resetForm)
        }
      },
      deep: true
    }
  },
  created () {
    if (Object.keys(this.dialogForm).length > 0) {
      this.editPositionData = JSON.parse(JSON.stringify(this.dialogForm))
    }
  }
}
</script>
<style scoped lang='scss'>
</style>
